package model

import (
	"gin-vben-admin/internal/constant"
	"gorm.io/gorm"
	"time"
)

type SysRole struct {
	RoleId   int64  `json:"role_id" gorm:"primaryKey;autoIncrement;comment:主键编码"`             //角色id
	RoleName string `json:"role_name"  gorm:"column:role_name;size:100;notNull;comment:角色名称"` //角色名称

	Sort      int             `json:"sort" gorm:"column:sort;notNull;default:0;comment:排序，小到大"`        //排序
	Remark    string          `json:"remark" gorm:"column:remark;notNull;default:'';comment:备注"`       //备注
	Status    constant.Status `json:"status" gorm:"column:status;notNull;default:1;comment:状态"`        //状态 1启用2禁用
	CreateBy  int64           `json:"create_by" gorm:"column:create_by;notNull;default:0;comment:创建者"` //创建者
	UpdateBy  int64           `json:"update_by" gorm:"column:update_by;notNull;default:0;comment:更新者"` //最后更新者
	CreatedAt time.Time       `json:"created_at" gorm:"column:created_at;type:datetime;notNull"`
	UpdatedAt time.Time       `json:"updated_at" gorm:"column:updated_at;type:datetime;notNull"`
	DeletedAt gorm.DeletedAt  `json:"-" gorm:"column:deleted_at;index;"`

	MenuIds []int64 `json:"menu_ids" gorm:"-"` //绑定的菜单id

	Menus []*SysMenu `json:"menus" gorm:"many2many:sys_role_menu;foreignKey:RoleId;joinForeignKey:role_id;references:MenuId;joinReferences:menu_id;"` //绑定的菜单
}

func (s *SysRole) TableName() string {
	return "sys_role"
}

func (s *SysRole) SetMenuIds() {
	total := len(s.Menus)
	s.MenuIds = make([]int64, total)
	for i := 0; i < total; i++ {
		s.MenuIds[i] = s.Menus[i].MenuId
	}
}
